<?php
namespace App\Model;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class RechargeBlacklist extends BaseModel
{
    protected $connection = 'api';
    protected $table = 'recharge_blacklist';
    protected $shortBlackList = 'recharge_five_minutes_blacklist';
    protected $userTable = 'user';

    //充值黑名单列表
    public function rechargeBlackList(Request $request)
    {
        $limit = $request->input('limit', 10);
        $page = $request->input('page', 1);
        $user_id = $request->input('user_id');
        $uuid = $request->input('uuid');
        $status = $request->input('status',2);

        $where = function($q)use($user_id, $status) {
            if(!empty($user_id)){
                $q->where('a.user_id',$user_id);
            }
            if($status != 2){
                $q->where('a.is_delete',$status);
            }
        };

        $result = DB::connection($this->connection)->table($this->table.' as a')
            ->leftJoin($this->userTable.' as b','a.user_id','b.user_id')
            ->where($where)
            ->paginate($limit, ['a.id','a.user_id','b.uuid','b.nickname','a.content','a.created_at','a.pay_type','a.updated_at','a.is_delete'], 'page', $page);
        $result = objToArray($result);

        return $result;
    }

    //充值黑名单新增
    public function rechargeBlackAdd($data){
        $res = curl_request(getKkaudioApiUrl() . '/inner/rechargeBlack/add', $data, 'json', 'POST');
        return curlResultHandle($res);
    }

    //充值黑名单删除
    public function rechargeBlackDel($data){
        $res = curl_request(getKkaudioApiUrl() . '/inner/rechargeBlack/del', $data, 'json', 'POST');
        return curlResultHandle($res);
    }

    //临时充值黑名单新增
    public function shortRechargeBlackAdd($data){
        $res = curl_request(getKkaudioApiUrl() . '/inner/rechargeBlack/shortRechargeBlackAdd', $data, 'json', 'POST');
        return curlResultHandle($res);
    }

    //临时充值黑名单删除
    public function shortRechargeBlackDel($data){
        $res = curl_request(getKkaudioApiUrl() . '/inner/rechargeBlack/shortRechargeBlackDel', $data, 'json', 'POST');
        return curlResultHandle($res);
    }

    //临时充值黑名单列表
    public function shortRechargeBlackList($page, $limit, $user_id, $status){
        $where = function($q)use($page, $limit, $user_id, $status) {
            if(!empty($user_id)){
                $q->where('b.user_id',$user_id);
            }
            if($status != 2){
                $q->where('b.is_delete',$status);
            }
        };
        $res = DB::connection($this->connection)->table($this->shortBlackList.' as b')
                ->leftJoin($this->userTable.' as u','u.user_id','b.user_id')
                ->where($where)
                ->orderBy('created_at','DESC')
                ->paginate($limit,['b.id','b.user_id','u.uuid','u.nickname','b.content','b.log_time','b.updated_at','b.pay_type','b.is_delete'],'page',$page);
        $res = objToArray($res);
        return $res;
    }

}
